package com.youlu.campus.wechat.service.impl;

import com.youlu.campus.wechat.entity.WechatProfitSharingLog;
import com.youlu.campus.wechat.repositories.WechatProfitSharingLogRepository;
import com.youlu.campus.wechat.service.WechatProfitSharingLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 微信商户分账操作记录 Service 实现类
 */
@Service
public class WechatProfitSharingLogServiceImpl implements WechatProfitSharingLogService {

    @Autowired
    private WechatProfitSharingLogRepository profitSharingLogRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public WechatProfitSharingLog save(WechatProfitSharingLog profitSharingLog) {
        WechatProfitSharingLog wechatBrandProfitSharingLog = findByMerchantIdAndAccountId(profitSharingLog.getMerchantId(), profitSharingLog.getAccountId());
        if (Objects.nonNull(wechatBrandProfitSharingLog)) {
            return wechatBrandProfitSharingLog;
        }
        // 设置时间戳
        LocalDateTime now = LocalDateTime.now();
        if (StringUtils.isBlank(profitSharingLog.getId())) {
            profitSharingLog.setCreateTime(now);
        }
        profitSharingLog.setUpdateTime(now);
        return profitSharingLogRepository.save(profitSharingLog);
    }

    @Override
    public WechatProfitSharingLog findById(String id) {
        return profitSharingLogRepository.findById(id).orElse(null);
    }


    @Override
    public List<WechatProfitSharingLog> findByMerchantId(String merchantId) {
        return profitSharingLogRepository.findByMerchantId(merchantId);
    }

    @Override
    public List<WechatProfitSharingLog> findByAccountId(String accountId) {
        return profitSharingLogRepository.findByAccountId(accountId);
    }

    @Override
    public WechatProfitSharingLog findByMerchantIdAndAccountId(String merchantId, String accountId) {
        return profitSharingLogRepository.findByMerchantIdAndAccountId(merchantId, accountId);
    }

    @Override
    public long countByMerchantId(String merchantId) {
        return profitSharingLogRepository.countByMerchantId(merchantId);
    }

    @Override
    public void deleteByMerchantIdAndAccountId(String merchantId,String accountId) {
        profitSharingLogRepository.deleteByMerchantIdAndAccountId(merchantId, accountId);
    }


}